<?php
class SP_Database_QueryBuilder
{
	static public function insert($table, $fields)
	{
		foreach($fields as $field => $value)
		{
			$query="INSERT INTO `$table` ";
			if( $value!==null )
			{	
				if( $value===true )
				{
					$v.='TRUE,';
				}
				elseif( $value===false )
				{
					$v.='FALSE,';
				}
				elseif( $value==='%_NULL' )
				{
					$v.='NULL,';
				}
				else
				{
					$v.="'$value',";
				}
				$f.="`$field`,";
			}
		}
		$f=substr($f, 0, -1);
		$v=substr($v, 0, -1);
		$query.="($f) VALUES ($v)";
		return $query;
	}
	
	public static function update($table, $fields=array(), $where=array(), $limit=NULL)
	{
		$query="UPDATE `$table` SET ";
		foreach($fields as $field => $value)
		{	
			if( $value!==NULL ) 
			{
				if ($value==='_NULL')
				{
					$f.="`$field`=NULL,";
				}
				else
				{
					$f.="`$field`='$value',";
				}
			}
		}
		$f=substr($f, 0, -1);
		$query.=$f;
		if( count($where)>0 )
		{
			$query.=" WHERE ";
			foreach($where as $field => $value)
			{	
				$w.="(`$field`='$value') AND ";
			}
			$w=substr($w, 0, -5);
			$query.=$w;
		}
		if ($limit) $query.=" LIMIT $limit";
		return $query;
	}
}